package com.smart.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smart.platform.common.CustomException;
import com.smart.platform.common.Result;
import com.smart.platform.common.ocr.OcrOnlyText;
import com.smart.platform.entity.Students;
import com.smart.platform.entity.TeaClasses;
import com.smart.platform.entity.TextRespond;
import com.smart.platform.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/textRespond")
@Api(tags = "试卷信息添加以及学生作答添加")
public class TextRespondController {
    @Autowired
    private TextRespondService textRespondService;

    @Autowired
    private Ocrservice ocrservice;

    @Autowired
    private TeaClassesService teaClassesService;

    @Autowired
    private WenXinService wenXinService;

    @Autowired
    private StudentsService studentsService;

    @Autowired
    private ScoreService scoreService;


    /**
     * 获取当前登录教师所管理的班级信息
     *
     */
    @ApiOperation(value = "获取当前登录教师所管理的年级和班级信息", notes = "当前登录教师的工号通过session获取 前端不需要传递信息")
    @GetMapping("/StudentList")
    public Result<List<Students>> ClassesList(HttpServletRequest request) {
        //通过当前登录教师的session获取到的userId（工号）
        String userId = (String) request.getSession().getAttribute("userId");

        LambdaQueryWrapper<TeaClasses> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(TeaClasses::getUserId, userId);

        TeaClasses one = teaClassesService.getOne(queryWrapper);
        LambdaQueryWrapper<Students> StudentQueryWrapper = new LambdaQueryWrapper<>();
        StudentQueryWrapper.eq(Students::getGrade, one.getGrade())
                .eq(Students::getClasses, one.getClasses());

        List<Students> studentsList = studentsService.list(StudentQueryWrapper);
        //返回去的数据中前端需要接受的是grade和classes 用于后续的数据选择
        return Result.success(studentsList);
    }


    //添加上传内容请save  查看返回的文心评阅
    @ApiOperation(value = "学生作答添加接口", notes = "考次 科目 题目 图片url userId")
    @PostMapping("/save")
    public Result<String> save(@RequestBody TextRespond textRespond) {
        //问题的url

        List<OcrOnlyText> questionText = ocrservice.ocrText(textRespond.getQuestionImage());
        List<String> questionStr = new ArrayList<>();
        for (OcrOnlyText ocrText : questionText) {
            questionStr.add(ocrText.getText());
        }
        //将返回的OCR数据转化为字符串
        String question = String.join(" ", questionStr);
        textRespond.setQuestion(question);

        //作答的url
        List<OcrOnlyText> respondText = ocrservice.ocrText(textRespond.getRespondImage());
        List<String> respondStr = new ArrayList<>();
        for (OcrOnlyText ocrText : respondText) {
            respondStr.add(ocrText.getText());
        }
        //将返回的OCR数据转化为字符串
        String answer = String.join(" ", respondStr);
        textRespond.setAnswer(answer);

        int valueMax = textRespond.getMark();
        String comment = wenXinService.dealQuestion(question, valueMax, answer).getComment();
        textRespond.setComment(comment);
        textRespondService.save(textRespond);

        return Result.success(comment);
    }

    @ApiOperation(value = "成绩修改", notes = "需要学号 考次 科目 题号 以及修改的分数")
    @PutMapping("/update")
    public Result<String> update(@RequestBody TextRespond respond) {
        LambdaQueryWrapper<TextRespond> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TextRespond::getUserId, respond.getUserId())
                .eq(TextRespond::getState, respond.getState())
                .eq(TextRespond::getCourse, respond.getCourse())
                .eq(TextRespond::getNumber, respond.getNumber());

        TextRespond one = textRespondService.getOne(queryWrapper);
        if (one.getStatus() == 1) {
            throw new CustomException("学生信息已修改");
        }

        if (one.getMark() < respond.getScore()) {
            throw new CustomException("成绩修改错误");
        }
        //设置当前修改信息的状态为 1 若下次重复修改 则会报出业务异常
        respond.setStatus(1);

        textRespondService.update(respond, queryWrapper);

        scoreService.update(respond);

        return Result.success("修改成功");
    }
}
